//
LPRECT *r;
HDC hDC;
	PAINTSTRUCT Ps;
	HWND hwnd_paint;
//
const char g_szClassName[] = "BasicApp1";
char AppCaption[40];
char edit_text[100];
int nr_cuvinte;
int nrcv=0,nc=0;
struct iv{
	int a;
	int b;
};

struct tree_index{
		int val;
		char str[11];
		bool apare;
		//
		int index;
		//
		tree_index *left;
		tree_index *right;

		iv interval;
	
};
tree_index *tree=NULL;
tree_index **elem;

char buffer_node[25];
//
void draw_rectangle(RECT *r) //exemplu: voi apela functia draw_rectangle(r[3])
{
	int left,top,right,bottom;
	left=r->left-10;
	top=r->top-10;
	right=r->right+20;
	bottom=r->bottom+0;

	Rectangle(hDC,left,top,right,bottom);
}
void draw_conex(RECT *tata,  RECT *frate)
{
	MoveToEx(hDC  ,tata->right-26  ,tata->bottom,NULL);  //26 este jumatatea lui 52[lungimea unui dreptunghi]
	LineTo(hDC  ,frate->left+26  ,frate->top-10);
}

void write_r(RECT *rectgl,LPCSTR text)
{
	int n=strlen(text);
	DrawText(hDC,text,n,rectgl,DT_CENTER);
}
void make_tree()
{
	int i;
	tree_index *p;
	///////////////////////////////////////////////////
	//initializarea coordonatelor pentru arbore => arbore STATIC
	r=(LPRECT*)malloc(15*sizeof(LPRECT));
	for(int i=0;i<15;i++)
		r[i]=(LPRECT)malloc(sizeof(RECT));

		r[0]->left=365;r[0]->top=68;r[0]->right=417;r[0]->bottom=85; //root ****MODIFICAT
		r[1]->left=172;r[1]->top=115;r[1]->right=231;r[1]->bottom=135;
		r[2]->left=578;r[2]->top=115;r[2]->right=645;r[2]->bottom=135;
		r[3]->left=74;r[3]->top=167;r[3]->right=133;r[3]->bottom=186;
		r[4]->left=277;r[4]->top=167;r[4]->right=336;r[4]->bottom=186;
		r[5]->left=497;r[5]->top=167;r[5]->right=556;r[5]->bottom=186;
		r[6]->left=686;r[6]->top=167;r[6]->right=745;r[6]->bottom=186;
		r[7]->left=38;r[7]->top=221;r[7]->right=88;r[7]->bottom=241;
		r[8]->left=126;r[8]->top=221;r[8]->right=185;r[8]->bottom=241;
		r[9]->left=226;r[9]->top=221;r[9]->right=285;r[9]->bottom=241;
		r[10]->left=323;r[10]->top=221;r[10]->right=382;r[10]->bottom=241;
		r[11]->left=440;r[11]->top=221;r[11]->right=499;r[11]->bottom=241;
		r[12]->left=537;r[12]->top=221;r[12]->right=596;r[12]->bottom=241;
		r[13]->left=636;r[13]->top=221;r[13]->right=695;r[13]->bottom=241;
		r[14]->left=733;r[14]->top=221;r[14]->right=792;r[14]->bottom=241;
	///////////////////////////////////////////////////////////////////////////////////////
p=(tree_index*)malloc(sizeof(tree_index));

	
elem=(tree_index**)malloc(15*sizeof(tree_index*));
	for( i=0;i<15;i++)
	{
		elem[i]=(tree_index*)malloc(sizeof(tree_index));
		elem[i]->left=elem[i]->right=NULL;
	}
	for(int i=0;i<15;i++)
	{
		elem[i]->val=i;
		elem[i]->apare=false; //initial nu apare niciun element al arborelui
	}
p=elem[0];
p->right=elem[2];

	p->right->left=elem[5];
	p->right->right=elem[6];
	p->right->right->right=elem[14];
	p->right->right->left=elem[13];
	p->right->left->left=elem[11];
	p->right->left->right=elem[12];
p->left=elem[1];
	p->left->left=elem[3];
	p->left->right=elem[4];
	p->left->left->left=elem[7];
	p->left->left->right=elem[8];
	p->left->right->right=elem[10];
	p->left->right->left=elem[9];
	tree=p;
}

int make_rope(char* text) 
{
	char *p;
	char buffer[200];
	strcpy(buffer,text);
p=strtok(buffer," ");

		while(p)
		{
			if(strlen(p)>8){
				return -1;
			}
			strcpy(a[nrcv],p);
			p=strtok(NULL," ");
			nrcv++;
		}
		nr_cuvinte=nrcv;
	if(nrcv>8)
	{
		nrcv=0;
		nr_cuvinte=0;
	return -1;
	}

elem[0]->apare=true;
int init;
for(int i=0;i<nrcv-1;i++)
{
	init =0;
	for(int j=2*i+1;j<2*i+3;j++)
	{
		if(j>=2*nrcv-1)
		break;
		if(init==2)
			break;
	  elem[j]->apare=true;
	  init++;
	}
}
nrcv=0;
return 1;
}




void RSD(tree_index *root)  //FUNCTIA care CREAZA arborele
{
if(root==NULL)
return;
	RSD(root->left);
elem[root->val]->index=0;
	if((root->left!=NULL && root->right!=NULL)&&(root->left->apare==false && root->right->apare==false )&&(root->apare==true))
	{ 
		strcpy(root->str,a[nc]);
						if(nc==0)
						{
							root->interval.a=0;
							root->interval.b=strlen(a[nc]);

						}
						else 
						{
							root->interval.a=nc;
							for(int i=0; i<nc; i++)
								root->interval.a=root->interval.a+strlen(a[i]);
							root->interval.b=root->interval.a+strlen(a[nc]);

						}
		root->index=nc+1;
		nc++;
		return;
	}
	if((root->left== NULL && root->right== NULL)&&(root->apare==true))
	{
		strcpy(root->str,a[nc]);
		if(nc==0)
		{
			root->interval.a=0;
			root->interval.b=strlen(a[nc]);


		}
		else 
		{
			root->interval.a=nc;
			for(int i=0; i<nc; i++)
				root->interval.a=root->interval.a+strlen(a[i]);
			root->interval.b=root->interval.a+strlen(a[nc]);


		}
		root->index=nc+1;
		nc++;
		return;
	}
	if((root->left!=NULL && root->right!=NULL)&&(root->left->apare==true || root->right->apare==true))
	{
		
		strcpy(root->str,"CONCAT");
	}
RSD(root->right);
}
void DRAWtree(tree_index *root,HWND hWnd)
{
if(root==NULL)
return;
DRAWtree(root->left,hWnd);
elem[root->val]->index=0;
	if((root->left!=NULL && root->right!=NULL)&&(root->left->apare==false && root->right->apare==false )&&(root->apare==true))
	{ 
		draw_rectangle(r[root->val]);
		if(strcmp(find_c,root->str)==0)	
			SetTextColor(hDC,RGB(0,0,128));
		write_r(r[root->val],root->str);
		UpdateWindow(hWnd);
		root->index=nc+1;
		nc++;
		return;
	}
	if((root->left== NULL && root->right== NULL)&&(root->apare==true))
	{	
		draw_rectangle(r[root->val]);
		if(strcmp(find_c,root->str)==0)	
			SetTextColor(hDC,RGB(0,0,128));
		write_r(r[root->val],root->str);
		UpdateWindow(hWnd);
		root->index=nc+1;
		nc++;
		return;
	}
	if((root->left!=NULL && root->right!=NULL)&&(root->left->apare==true || root->right->apare==true))
	{
		draw_rectangle(r[root->val]);	
		SetTextColor(hDC,RGB(0,0,0));
		write_r(r[root->val],"CONCAT");
		UpdateWindow(hWnd);
		SetTextColor(hDC,RGB(112,112,112));
	}
DRAWtree(root->right,hWnd);
}
void draw_final_conex()
{nc=0;
if(tree==NULL) return;
	int i,j;
	for(i=0;i<7;i++)
			for(j=2*i+1;j<2*i+3;j++)
			if(elem[j]->apare==true)
			draw_conex(r[i],r[j]);
}

void DisplayInfo(char *filename,HDC hDc)
{
	int i;
	char buffer[100];
	FILE *f1=fopen(filename,"rt");
	LPRECT *rtg;
	
	rtg=(LPRECT*)malloc(7*sizeof(LPRECT));
	for(i=0;i<7;i++)
		rtg[i]=(LPRECT)malloc(sizeof(RECT));
	for(i=0;i<7;i++)
	{
		rtg[i]->left=29;rtg[i]->top=51+(37*i);rtg[i]->right=800;rtg[i]->bottom=74+(37*i); 
				
	}
i=0;
	SetTextColor(hDC,RGB(176,23,31));
while(!feof(f1))
	{
		fgets(buffer,100,f1);
		DrawText(hDC,(LPCSTR)buffer,strlen(buffer),rtg[i],DT_INTERNAL);
		i++;

	}
}
void init_boundaries(tree_index *root)
{
	if(root==NULL)
	return;

	init_boundaries(root->left);
		
	if((root->left!=NULL) && (root->left->apare==true) &&(strcmp(root->str,"concat")!=0 ))
	{
		root->interval.a=root->left->interval.a;
		root->interval.b=root->right->interval.b;
	}

	init_boundaries(root->right);
		

}
void concat_short(tree_index *root,char *str)
{
	if(root->apare==false)
		return;
if(root->left!=NULL)
{
	if(root->left->apare==false)
	{
		strcat(root->str,str);
		strcat(a[nr_cuvinte-1],str);
		strcat(textinput,str);
		return;
	}
}else 
		{
			strcat(root->str,str);
				strcat(a[nr_cuvinte-1],str);
				strcat(textinput,str);
				return;
		}
	
	concat_short(root->right,str);
}


void mid_insert(tree_index *root, int a,int b,char* str)
{
	if(root==NULL)
		return;
	if(root->interval.b < b )
		return;
	if(root->interval.a > a)
		return;
mid_insert(root->left,a,b,str);
	if(root->interval.a <= a  &&  root->interval.b>= b  && root->left!=NULL && root->left->apare==false)
	{
		int a_sym=a - root->interval.a;
		int b_sym=b - root->interval.a;
		char buf1[10];
		strcpy(buf1,root->str + a_sym);
		root->str[b_sym-1]='\0';  //2 linii mai sus am eliminat dependenta de index
		strcat(root->str,str);			//indexul cuvantului din nodul arborelui NU va mai corespunde cu indexul a sau b
		strcat(root->str,buf1);

	}

	if((root->interval.a <= a  &&  root->interval.b>= b)&&(root->left== NULL && root->right== NULL)&&(root->apare==true))
	{
		int a_sym=a - root->interval.a;
		int b_sym=b - root->interval.a;
		char buf1[10];
		strcpy(buf1,root->str + a_sym);
		root->str[b_sym-1]='\0';  //2 linii mai sus am eliminat dependenta de index
		strcat(root->str,str);			//indexul cuvantului din nodul arborelui NU va mai corespunde cu indexul a sau b
		strcat(root->str,buf1);
	}
mid_insert(root->right,a,b,str);

}


void free_tree(tree_index* root) 
{
	if(root==NULL)
		return;
	free_tree(root->left);
	free_tree(root->right);
	if(root->right==NULL && root->left==NULL)
		free(root);
}


void free_memory()
{
	if(tree==NULL)
		return;
	for(int i=14;i>=0;i--)
	{
		free(r[i]);
		free(elem[i]);
	}
}
//
BOOL LoadBitmapFromBMPFile( LPTSTR szFileName, HBITMAP *phBitmap,
   HPALETTE *phPalette )
   {

   BITMAP  bm;

   *phBitmap = NULL;
   *phPalette = NULL;

   // Use LoadImage() to get the image loaded into a DIBSection
   *phBitmap = (HBITMAP)LoadImage( NULL, szFileName, IMAGE_BITMAP, 0, 0,
               LR_CREATEDIBSECTION | LR_DEFAULTSIZE | LR_LOADFROMFILE );
   if( *phBitmap == NULL )
     return FALSE;

   // Get the color depth of the DIBSection
   GetObject(*phBitmap, sizeof(BITMAP), &bm );
   // If the DIBSection is 256 color or less, it has a color table
   if( ( bm.bmBitsPixel * bm.bmPlanes ) <= 8 )
   {
   HDC           hMemDC;
   HBITMAP       hOldBitmap;
   RGBQUAD       rgb[256];
   LPLOGPALETTE  pLogPal;
   WORD          i;

   // Create a memory DC and select the DIBSection into it
   hMemDC = CreateCompatibleDC( NULL );
   hOldBitmap = (HBITMAP)SelectObject( hMemDC, *phBitmap );
   // Get the DIBSection's color table
   GetDIBColorTable( hMemDC, 0, 256, rgb );
   // Create a palette from the color tabl
   pLogPal = (LOGPALETTE *)malloc( sizeof(LOGPALETTE) + (256*sizeof(PALETTEENTRY)) );
   pLogPal->palVersion = 0x300;
   pLogPal->palNumEntries = 256;
   for(i=0;i<256;i++)
   {
     pLogPal->palPalEntry[i].peRed = rgb[i].rgbRed;
     pLogPal->palPalEntry[i].peGreen = rgb[i].rgbGreen;
     pLogPal->palPalEntry[i].peBlue = rgb[i].rgbBlue;
     pLogPal->palPalEntry[i].peFlags = 0;
   }
   *phPalette = CreatePalette( pLogPal );
   // Clean up
   free( pLogPal );
   SelectObject( hMemDC, hOldBitmap );
   DeleteDC( hMemDC );
   }
   else   // It has no color table, so use a halftone palette
   {
   HDC    hRefDC;

   hRefDC = GetDC( NULL );
   *phPalette = CreateHalftonePalette( hRefDC );
   ReleaseDC( NULL, hRefDC );
   }
   return TRUE;

   }
//

